package cn.lili.controller.payment;

import cn.lili.common.security.AuthUser;
import cn.lili.common.security.context.UserContext;
import cn.lili.common.security.enums.SceneEnums;
import cn.lili.common.utils.ResultUtil;
import cn.lili.common.vo.PageVO;
import cn.lili.common.vo.ResultMessage;
import cn.lili.modules.member.entity.vo.MemberPointsHistoryVO;
import cn.lili.modules.payment.entity.dos.UserPointsHistory;
import cn.lili.modules.wallet.service.UserPointsHistoryService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Objects;

/**
 * 用户积分历史接口
 *
 * @author Bulbasaur
 * @since 2020-02-25 14:10:16
 */
@RestController
@Api(tags = "用户积分历史接口")
@RequestMapping("/payment/points/history")
@RequiredArgsConstructor
public class UserPointsHistoryController {
    private final UserPointsHistoryService userPointsHistoryService;

    @ApiOperation(value = "分页获取")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "会员ID", required = true, paramType = "query"),
            @ApiImplicitParam(name = "username", value = "会员名称", required = true, paramType = "query")
    })
    @GetMapping(value = "/page")
    public ResultMessage<Page<UserPointsHistory>> getByPage(PageVO page, String userId, String username) {
        AuthUser authUser = Objects.requireNonNull(UserContext.getCurrentUser());
        if (authUser.getScene().equals(SceneEnums.MANAGER)) {
            return ResultUtil.data(userPointsHistoryService.pointsHistoryList(page, userId, username));
        } else {
            return ResultUtil.data(userPointsHistoryService.pointsHistoryList(page,
                    UserContext.getCurrentUser().getExtendId(), null));
        }
    }

    @ApiOperation(value = "获取用户积分VO")
    @ApiImplicitParam(name = "memberId", value = "会员ID", paramType = "query")
    @GetMapping
    public ResultMessage<MemberPointsHistoryVO> getMemberPointsHistoryVO(String memberId) {
        if (UserContext.getCurrentUser().getScene().equals(SceneEnums.MANAGER)) {
            return ResultUtil.data(userPointsHistoryService.pointsHistoryVO(memberId));
        } else {
            return ResultUtil.data(userPointsHistoryService.pointsHistoryVO(UserContext.getCurrentUser().getExtendId()));
        }
    }


}
